package com.psa.element;

import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Color;
import com.google.gson.Gson;
import com.psa.system.R;
import com.psa.tool.DBTool;
import com.psa.tool.SystemStatus;

import java.math.BigDecimal;

/**
 *
 * @author david
 * @date 2014-06-10
 */
public class OrderItem implements IJson,ISqlObject{
    public int _id = 0;
    public Merchandise merchandise = null;
    public String OrderId = "";
    public String QTYText = "";
    public SalesType ST = new SalesType(SalesType.CashSales, SalesType.getName(SalesType.CashSales));
    public int QTY = 0;
    public float Price = 0;
    public float Sum = 0;
    public String PromoCode = "";
    public int Status = 0;
    public String StatusMsg = "";

    public static final int ErrorBgColor = Color.parseColor("#a6ff84");


    @Override
    public String toJson() {
        return new Gson().toJson(this);
    }

    @Override
    public int insert(DBTool dbTool) {
        return (int)dbTool.insert("OrderItem", createValues());
    }

    @Override
    public int update(DBTool dbTool) {
        ContentValues values = createValues();
        String where = "_id = ?";
        String[] value = { String.valueOf(_id) };
        return dbTool.update("OrderItem", values, where, value);
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "_id = ?";
        String[] value = { String.valueOf(_id) };
        return dbTool.delete("OrderItem", where, value);
    }

    /*
    _id INTEGER PRIMARY KEY AUTOINCREMENT,MerchandiseID nvarchar(12),"
                + "OrderCode nvarchar(15),QTYText nvarchar(10),SalesType nvarchar(10),QTY int,Price float,Sum float,PromoCode nvarchar(20),Status int
     */

    @Override
    public ISqlObject full(Cursor cursor) {
        if (cursor!=null && cursor.moveToNext()) {
            OrderItem orderItem = new OrderItem();
            orderItem._id = cursor.getInt(0);
            orderItem.merchandise = Merchandise.getMerchandise(cursor.getString(1));
            orderItem.OrderId = cursor.getString(2);
            orderItem.QTYText = cursor.getString(3);
            orderItem.ST = new SalesType(cursor.getString(4), SalesType.getName(cursor.getString(4)));
            orderItem.QTY = cursor.getInt(5);
            orderItem.Price = cursor.getFloat(6);
            orderItem.Sum = cursor.getFloat(7);
            orderItem.PromoCode = cursor.getString(8);
            orderItem.PromoCode = orderItem.PromoCode.equals("null")? "" : orderItem.PromoCode;
            orderItem.Status = cursor.getInt(9);
            orderItem.StatusMsg = cursor.getString(10);
            return orderItem;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {
        dbTool.delete("OrderItem", "_id Like ?", new String[]{"%"});
    }

    @Override
    public String getDataSql() {
        return "";
    }

    @Override
    public String getSyncMethod() {
        return "";
    }

    @Override
    public String toString() {
        return merchandise.NameChinese + "\n "+ SystemStatus.MainActivity.getString(R.string.order_count) + QTYText+ "\n "
                + SystemStatus.MainActivity.getString(R.string.order_sum)+SystemStatus.MainActivity.getString(R.string.currency_sign) + Sum
                + "\n "+ SystemStatus.MainActivity.getString(R.string.order_item_type) + ST.TypeName;
    }

    public float sum(){
        if (ST !=null && ST.TypeCode.equals(SalesType.CashSales) && merchandise != null) {
            Price = Price != 0 && !Double.isNaN(Price)? Price : merchandise.Price;
            Sum = Price / merchandise.Packing * QTY;
            BigDecimal bg = new BigDecimal(Sum);
            Sum = bg.setScale(6, BigDecimal.ROUND_HALF_UP).floatValue();
            return Sum;
        }
        return 0.00f;
    }

    public String getQTYText() {
        if (QTYText.equals("") && merchandise != null){
            int xx = QTY / merchandise.Packing;
            int zz = QTY % merchandise.Packing;
            QTYText = xx + "-" + zz;
        }
        return QTYText;
    }

    private ContentValues createValues() {
        ContentValues values = new ContentValues();
        values.put("MerchandiseID", merchandise.ID.trim());
        values.put("OrderId", OrderId);
        values.put("QTYText", QTYText);
        values.put("SalesType", ST.TypeCode);
        values.put("QTY", QTY);
        values.put("Price", Price);
        values.put("Sum", Sum);
        values.put("PromoCode", PromoCode.equals("null")?"":PromoCode.trim());
        values.put("Status", Status);
        values.put("StatusMsg", StatusMsg.trim());
        return values;
    }
}
